package com.qf.dao.impl;

import com.qf.dao.UserDao;
import com.qf.domain.Address;
import com.qf.domain.User;
import com.qf.utils.DataSourceUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/*
 *                             _ooOoo_
 *                            o8888888o
 *                            88" . "88
 *                            (| -_- |)
 *                            O\  =  /O
 *                         ____/`---'\____
 *                       .'  \\|     |//  `.
 *                      /  \\|||  :  |||//  \
 *                     /  _||||| -:- |||||-  \
 *                     |   | \\\  -  /// |   |
 *                     | \_|  ''\---/''  |   |
 *                     \  .-\__  `-`  ___/-. /
 *                   ___`. .'  /--.--\  `. . __
 *                ."" '<  `.___\_<|>_/___.'  >'"".
 *               | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 *               \  \ `-.   \_ __\ /__ _/   .-` /  /
 *          ======`-.____`-.___\_____/___.-`____.-'======
 *                             `=---='
 *          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 *                     佛祖保佑        永无BUG
 *            佛曰:
 *                   写字楼里写字间，写字间里程序员；
 *                   程序人员写程序，又拿程序换酒钱。
 *                   酒醒只在网上坐，酒醉还来网下眠；
 *                   酒醉酒醒日复日，网上网下年复年。
 *                   但愿老死电脑间，不愿鞠躬老板前；
 *                   奔驰宝马贵者趣，公交自行程序员。
 *                   别人笑我忒疯癫，我笑自己命太贱；
 *                   不见满街漂亮妹，哪个归得程序员？
 */
public class UserDaoImpl implements UserDao {
    private QueryRunner qr=new QueryRunner();
    @Override
    public void insert(User user) {
        String sql="insert into tb_user(username,password,email,gender,flag,role,code) values(?,?,?,?,?,?,?);";
        Object[] params={user.getUsername(),user.getPassword(),user.getEmail(),user.getGender(),user.getFlag(),user.getRole(),user.getCode()};
        Connection conn= DataSourceUtils.getConnection();
        try {
            qr.update(conn, sql,params);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            DataSourceUtils.closeAll(null,null,conn);
        }

    }

    @Override
    public User select(String username, String password) {
        String sql="select id,username,password,email,gender,flag,role,code from tb_user where username=? and password=?";
        Connection conn= DataSourceUtils.getConnection();
        try {
            return qr.query(conn, sql, new BeanHandler<>(User.class),username,password);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            DataSourceUtils.closeAll(null,null,conn);
        }

    }

    @Override
    public boolean select(String username) {
        String sql="select id,username,password,email,gender,flag,role,code from tb_user where username=?";
        Connection conn= DataSourceUtils.getConnection();
        try {
            User user=qr.query(conn, sql, new BeanHandler<>(User.class),username);
            if(user==null){
                return false;
            }
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            DataSourceUtils.closeAll(null,null,conn);
        }
    }

    @Override
    public List<Address> selectAddress(Integer uid) {
        String sql="select id,detail,name,phone,uid,level from tb_address where uid=? and level!=2";
        Connection conn= DataSourceUtils.getConnection();
        try {
            return qr.query(conn, sql,new BeanListHandler<>(Address.class),uid);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally {
            DataSourceUtils.closeAll(null,null,conn);
        }

    }

    @Override
    public void insertAddress(Address address) {
        String sql="insert into tb_address(detail,name,phone,uid,level) values(?,?,?,?,?);";
        Object[] params={address.getDetail(),address.getName(),address.getPhone(),address.getUid(), address.getLevel()};
        Connection conn= DataSourceUtils.getConnection();
        try {
            qr.update(conn, sql,params);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            DataSourceUtils.closeAll(null,null,conn);
        }
    }

    @Override
    public void updateAddStatus(Integer uid,Integer aid) {
        String sql1="update tb_address set level=0 where uid=?";
        String sql2="update tb_address set level=1 where id=?";
        Connection conn= DataSourceUtils.getConnection();
        try {
            qr.update(conn, sql1,uid);
            qr.update(conn, sql2,aid);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            DataSourceUtils.closeAll(null,null,conn);
        }
    }

    @Override
    public void updateAdd(Address address) {
        String sql="update tb_address set detail=?,name=?,phone=?, level=? where id=?";
        Object[] params={address.getDetail(),address.getName(),address.getPhone(),address.getLevel(),address.getId()};
        Connection conn= DataSourceUtils.getConnection();
        try {
            qr.update(conn, sql,params);

        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            DataSourceUtils.closeAll(null,null,conn);
        }
    }

    @Override
    public void deleteAddress(int aid) {
        //String sql="delete from tb_address where id=?";
        String sql="update tb_address set level=2 where id=?";
        Connection conn= DataSourceUtils.getConnection();
        try {
            qr.update(conn, sql,aid);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            DataSourceUtils.closeAll(null,null,conn);
        }
    }
}
